use serde::{Deserialize, Serialize};

use crate::database::{self, table::category::model::NewCategory};

#[derive(Debug, Serialize, Deserialize)]
pub struct CategoryForm {
    pub id: i32,
    pub name: String,
    pub desc: String,
    pub cover: String,
    pub slug: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct NewCategoryForm {
    pub name: String,
    pub desc: String,
    pub cover: String,
    pub slug: String,
}

impl From<&database::table::category::model::Category> for CategoryForm {
    fn from(category: &database::table::category::model::Category) -> Self {
        CategoryForm {
            id: category.id,
            name: category.name.clone(),
            desc: category.desc.clone(),
            cover: category.cover.clone(),
            slug: category.slug.clone(),
        }
    }
}

impl CategoryForm {
    pub fn to(&self) -> NewCategoryForm {
        NewCategoryForm {
            name: self.name.clone(),
            desc: self.desc.clone(),
            cover: self.cover.clone(),
            slug: self.slug.clone(),
        }
    }
}

impl NewCategoryForm {
    pub fn to(&self) -> NewCategory {
        NewCategory {
            name: &self.name,
            desc: &self.desc,
            cover: &self.cover,
            slug: &self.slug,
        }
    }
}
